package com.google.code.sysarch.v4.vrml_loader;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;

import com.google.code.sysarch.v4.main.BattleShips;

public class VRML_Loader extends Thread {

	private VRML_Definitions ref;
	private VRML_Model model;
	private HashMap<String, VRML_Block> defs = new HashMap<String, VRML_Block>();
	private FileInputStream inStream;
	private int bytesDone;
	
	public VRML_Loader(VRML_Definitions ref) {
		this.bytesDone = 0;
		this.ref = ref;
	}
	
	public VRML_Model getModel() {
		return this.model;
	}
	
	public VRML_Definitions getRef() {
		return this.ref;
	}
	
	public VRML_Block getDef(String name) {
		return this.defs.get(name);
	}
	
	public void setDef(String name, VRML_Block block) {
		this.defs.put(name, block);
	}
	
	public int getBytes() {
		return this.bytesDone;
	}	
	
	public VRML_Model loadModel(String filename) {
		model = new VRML_Model();
		try {
			BattleShips.loadingWnd.setState("Loading '"+filename+"'");
			this.bytesDone += new File(filename).length();
			inStream = new FileInputStream(filename);
			VRML_Block block = new VRML_Block("_root", "_root", this, true);
			block.parseVRML(inStream);
			inStream.close();
			block.executeVRML(null);
		} catch (FileNotFoundException e) {
			System.out.println("Error in VRML_Loader: File '"+filename+"' not found");
		} catch (IOException e) {
			System.out.println("Error in VRML_Loader: IOException");
		}
		return model;
	}
	
	public static void error(String message) {
		System.out.println("Error in VRML_Loader: "+message);
		System.exit(1);
	}
	
}
